<?php

namespace controller;

/**
 * 控制器的基类
 * Class Controller
 */

class Controller{
    public function checkLogin()
    {
        session_start();
        if (empty($_SESSION['admin_email'])) {
            echo '尚未登录，请先登录 <a href="index.php?c=login">登录</a>';
            exit();
        }
    }

    public function validate($field, $min, $max, $message)
    {
        if (mb_strlen($field) < $min || mb_strlen($field) > $max) {
            echo $message;
            echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
            exit();
        }
    }

    /**
     * 没有减少代码量，方便后续换模板
     *
     * @param $templatePath
     * @param array $templateVars
     */
    public function display($templatePath, $templateVars = [])
    {
        extract($templateVars); // ********
        include_once APP_PATH . "./view/{$templatePath}.php";
    }
}